package Threadprojects.pro1_matrix.rowpool;

import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RowPoolMulitply {
    public static void multiply(double[][] m1, double[][] m2, double[][] rs) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                20, // 核心线程数
                40, // 最大线程数
                20, // 空闲线程超时时间
                TimeUnit.SECONDS, // 超时时间单位
                new ArrayBlockingQueue<>(2000), // 任务队列
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
        );

        ArrayList<Thread> list = new ArrayList<>();
        int m1Rows = m1.length;
        int m1Cols = m1[0].length;
        int m2Cols = m2[0].length;

        for (int i = 0; i < m1Rows; i++) {
            //把每一行的计算拆分未一次线程里面
            executor.submit(new RowTask(rs, m1, m2, i));
        }
        // 关闭线程池
        executor.shutdown();

        try {
            // 等待所有任务完成
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
